//
//  WPCDBManager.m
//  Wallpaper.co
//
//  Created by zhou_yuepeng on 2017/9/19.
//  Copyright © 2017年 www.dahuatech.com. All rights reserved.
//

#import "WPCDBManager.h"
#import "WPCThemesInfo.h"
#import "FMDB/FMDB.h"

static NSString* const SC_TABLE_FAVORITE = @"favorite";

@implementation WPCDBManager

+ (BOOL)addFavoriteAtItem:(WPCThemesItem*)item
{
    BOOL ret = NO;
    FMDatabase *db = [FMDatabase databaseWithPath:[self favotiteDBPath]];
    if (![db open]) {
        return ret;
    }
    
    NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('id' INTEGER PRIMARY KEY   AUTOINCREMENT, 'title' TEXT UNIQUE, 'thumbnailURL' TEXT, 'originalPicURL' TEXT)", SC_TABLE_FAVORITE];
    ret = [db executeUpdate:sql];
    if (!ret) {
        [db close];
        return ret;
    }
    
    sql = [NSString stringWithFormat:@"INSERT INTO '%@' ('title', 'thumbnailURL', 'originalPicURL') VALUES ('%@', '%@', '%@')", SC_TABLE_FAVORITE, item.title, item.thumbnailURL, item.originalPicURL];
    ret = [db executeUpdate:sql];
    if (!ret) {
        [db close];
        return ret;
    }
    
    [db close];
    return YES;
}

+ (BOOL)deleteFavoriteAtItem:(WPCThemesItem*)item
{
    BOOL ret = NO;
    FMDatabase *db = [FMDatabase databaseWithPath:[self favotiteDBPath]];
    if (![db open]) {
        return ret;
    }
    
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM '%@' WHERE title = '%@'", SC_TABLE_FAVORITE, item.title];
    ret = [db executeUpdate:sql];
    if (!ret) {
        [db close];
        return ret;
    }
    
    [db close];
    return YES;
}

+ (BOOL)queryFavoriteWithTitle:(NSString*)title
{
    BOOL ret = NO;
    FMDatabase *db = [FMDatabase databaseWithPath:[self favotiteDBPath]];
    if (![db open]) {
        return ret;
    }
    
    NSString *sql = [NSString stringWithFormat:@"SELECT COUNT(*) FROM '%@' WHERE title = '%@'", SC_TABLE_FAVORITE, title];
    int count = [db intForQuery:sql];
    [db close];
    
    return count != 0;
}

+ (NSArray*)queryFavoriteItemWithRange:(NSRange)range
{
    FMDatabase *db = [FMDatabase databaseWithPath:[self favotiteDBPath]];
    if (![db open]) {
        return nil;
    }
    
    NSString *sql = [NSString stringWithFormat:@"SELECT COUNT(*) FROM '%@'", SC_TABLE_FAVORITE];
    int totalCount = [db intForQuery:sql];
    int totalPage = totalCount / 12;
    int remainder = totalCount % 12;
    totalPage += (remainder != 0 ? 1 : 0);
    
    NSMutableArray *ret = [NSMutableArray arrayWithCapacity:5];
    sql = [NSString stringWithFormat:@"SELECT * FROM '%@' ORDER BY id DESC LIMIT %ld,%ld", SC_TABLE_FAVORITE, range.location, range.length];
    FMResultSet *result = [db executeQuery:sql];
    while ([result next]) {
        WPCThemesItem *model = [[WPCThemesItem alloc] init];
        model.title = [result stringForColumn:@"title"];
        model.thumbnailURL = [NSURL URLWithString:[result stringForColumn:@"thumbnailURL"]];
        model.originalPicURL = [NSURL URLWithString:[result stringForColumn:@"originalPicURL"]];
        model.totalpage = totalPage;
        
        [ret addObject:model];
    }
    
    return ret;
}

#pragma mark <private method>
+ (NSString*)favotiteDBPath
{
    NSString *doc =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dbPath = [doc stringByAppendingPathComponent:@"wallpaper.db"];
    
    return dbPath;
}

@end
